package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.DatiInfo;
import com.xgkj.diyiketang.util.DialogUtil;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/10/9.
 */
public class MyChuTiAdapter extends BaseAdapter {
    private Context mContext;
    private List<DatiInfo> mList = new ArrayList<DatiInfo>();

    public MyChuTiAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<DatiInfo> list){
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void clearData(){
        mList.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mList.size()>0){
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList.size()>0){
            return mList.get(position);
        }
        return null;
    }


    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.my_chuti_item, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews(mList.get(position), (ViewHolder) convertView.getTag());
//        final DatiInfo info = mList.get(position);
//        if (info != null){
//            convertView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Intent detailIntent = new Intent(mContext, DatiDetailActivity.class);
//                    detailIntent.putExtra("DATIINFO",info);
//                    mContext.startActivity(detailIntent);
//                }
//            });
//        }
        return convertView;
    }

    private void initializeViews(final DatiInfo info,ViewHolder viewHolder){

        String url = info.getQuestionImage();
        if (url != null && url.length()>0){
            ViewUtil.Size size = new ViewUtil.Size(ViewUtil.getScreenWidth(mContext),0);
            String cacheKey = ImageCache.getInstance(mContext).getCacheKey(url,size);
            Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(cacheKey);
            if (cacheBitmap !=null){
                viewHolder.datiImage.setImageBitmap(cacheBitmap);
            }else {
                viewHolder.datiImage.setImageResource(R.drawable.zhanweitu);
                ImageCache.getInstance(mContext).loadBitmapToView(viewHolder.datiImage,url,size);
            }
        }


        String grade = null;
        String gradetype = info.getGrade();
        if (gradetype.equals("1")){
            grade = "一年级";
        }else if (gradetype.equals("2")){
            grade = "二年级";
        }else if (gradetype.equals("3")){
            grade = "三年级";
        }else if (gradetype.equals("4")){
            grade = "四年级";
        }else if (gradetype.equals("5")){
            grade = "五年级";
        }else if (gradetype.equals("6")){
            grade = "六年级";
        }else if (gradetype.equals("7")){
            grade = "初一";
        }else if (gradetype.equals("8")){
            grade = "初二";
        }else if (gradetype.equals("9")){
            grade = "初三";
        }else if (gradetype.equals("10")){
            grade = "高一";
        }else if (gradetype.equals("11")){
            grade = "高二";
        }else if (gradetype.equals("12")){
            grade = "高三";
        }

        String subjectNo = null;
        String subjectType = info.getSubjectNo();
        if (subjectType.equals("1")){
            subjectNo = "数学";
        }else if (subjectType.equals("2")){
            subjectNo = "外语";
        }else if (subjectType.equals("3")){
            subjectNo = "综合";
        }else if (subjectType.equals("0")){
            subjectNo = "语文";
        }

        String questionType = null;

        if (info.getQuestionType().equals("0")){
            questionType = "填空题";
        }else if (info.getQuestionType().equals("1")){
            questionType = "选择题";
        }
        viewHolder.datiTag.setText(grade+" "+subjectNo+" "+questionType);
//        String answerType = null;
//        if (info.getAnswerType().equals("0")){
//            answerType = "必答题";
//        }else if (info.getAnswerType().equals("1")){
//            answerType = "抢答题";
//        }
//        viewHolder.datiType.setText(answerType);
        viewHolder.datiType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.createDialog(mContext, -1, mContext.getString(R.string.prompt), mContext.getString(R.string.you_sure_delete_timu), mContext.getString(R.string.ok), mContext.getString(R.string.cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (mListener != null){
                            mListener.onDeleteClick(info);
                        }
                    }
                },null);
            }
        });
    }

    private OnAdapterClickListener mListener;
    public void setOnAdapterClickListener(OnAdapterClickListener listener){
        mListener = listener;
    }
    public interface OnAdapterClickListener{
        public void onDeleteClick(DatiInfo info);
    }
    protected class ViewHolder {
        private ImageView datiImage;
        private TextView datiType;
        private TextView datiTag;

        public ViewHolder(View view) {
            datiImage = (ImageView) view.findViewById(R.id.dati_image);
            datiType = (TextView) view.findViewById(R.id.dati_type);
            datiTag = (TextView) view.findViewById(R.id.dati_tag);
        }
    }
}
